package com.lord.view;

import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

import com.lord.action.GoddessAction;
import com.lord.model.Goddess;

public class View {

    private static final String CONTEXT = "欢迎来到女神禁区：\n" + "下面是女神禁区的功能列表：\n" + "[MAIN/M]:主菜单\n" + "[QUERY/Q]:查看全部女神的信息\n"
            + "[GET/G]:查看某位女神的详细信息\n" + "[ADD/A]:添加女神信息\n" + "[UPDATE/U]:更新女神信息\n" + "[DELETE/D]:删除女神信息\n"
            + "[SEARCH/S]:查询女神信息(根据姓名、手机号来查询)\n" + "[EXIT/E]:退出女神禁区\n" + "[BREAK/B]:退出当前功能，返回主菜单";

    private static final String OPERATION_MAIN = "MAIN";
    private static final String OPERATION_QUERY = "QUERY";
    private static final String OPERATION_GET = "GET";
    private static final String OPERATION_ADD = "ADD";
    private static final String OPERATION_UPDATE = "UPDATE";
    private static final String OPERATION_DELETE = "DELETE";
    private static final String OPERATION_SEARCH = "SEARCH";
    private static final String OPERATION_EXIT = "EXIT";
    private static final String OPERATION_BREAK = "BREAK";

    public static void main(String[] args) {
        System.out.println(CONTEXT);

        Scanner scan = new Scanner(System.in);
        Goddess goddess = new Goddess();
        GoddessAction action = new GoddessAction();
        String prenious = null;
        Integer step = 1;
        String queryKey = null;

        List<Map<String, Object>> params = new ArrayList<Map<String, Object>>();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("name", null);
        param.put("rela", "=");
        param.put("value", null);

        while (scan.hasNext()) {
            String in = scan.next().toString();

            if (OPERATION_MAIN.equals(in.toUpperCase()) || OPERATION_MAIN.substring(0, 1).equals(in.toUpperCase())) {

                System.out.println(CONTEXT);

            } else if (OPERATION_QUERY.equals(in.toUpperCase())
                    || OPERATION_QUERY.substring(0, 1).equals(in.toUpperCase())) {

                try {
                    List<Goddess> goddesList = action.query();
                    for (Goddess info : goddesList) {
                        System.out.println("编号：" + info.getId() + "，姓名：" + info.getUser_name());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else if (OPERATION_GET.equals(in.toUpperCase()) || OPERATION_GET.substring(0, 1).equals(in.toUpperCase())
                    || OPERATION_GET.equals(prenious)) {

                prenious = OPERATION_GET;

                // 女神详情
                if (1 == step) {
                    System.out.println("请输入女神的编号，来查看女神的详细信息");
                } else if (2 == step) {
                    try {
                        Goddess info = action.get(Integer.valueOf(in));
                        if (info == null) {
                            System.out.println("您输入的编号不存在");
                            continue;
                        }
                        System.out.println("编号：" + info.getId() + "\n姓名：" + info.getUser_name() + "\n年龄："
                                + info.getAge() + "\n性别：" + (info.getSex() == 1 ? "男" : "女") + "\n生日："
                                + info.getBirthday() + "\n邮箱：" + info.getEmail() + "\n手机号：" + info.getEmail());
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    } finally {
                        // 重置
                        prenious = null;
                        step = 1;
                    }
                }

                if (OPERATION_GET.equals(prenious)) {
                    step++;
                }

            } else if (OPERATION_ADD.equals(in.toUpperCase()) || OPERATION_ADD.substring(0, 1).equals(in.toUpperCase())
                    || OPERATION_ADD.equals(prenious)) {

                prenious = OPERATION_ADD;

                if (OPERATION_BREAK.equals(in.toUpperCase())
                        || OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
                    System.out.println(CONTEXT);
                    // 重置
                    prenious = null;
                    step = 1;
                    continue;
                }

                // 新增女神
                if (1 == step) {
                    System.out.println("请输入女神的 [姓名] ");
                } else if (2 == step) {
                    goddess.setUser_name(in);
                    System.out.println("请输入女神的 [年龄] ");
                } else if (3 == step) {
                    goddess.setAge(Integer.valueOf(in));
                    System.out.println("请输入女神的 [生日] ， 格式如： yyyy-MM-dd");
                } else if (4 == step) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date birthday = null;
                    try {
                        birthday = sdf.parse(in);
                        goddess.setBirthday(birthday);
                        System.out.println("请输入女神的 [邮箱] ");
                    } catch (ParseException e) {
                        e.printStackTrace();
                        System.out.println("您输入的格式有误，请重新输入！");
                        step = 3;
                    }
                } else if (5 == step) {
                    goddess.setEmail(in);
                    System.out.println("请输入女神的 [手机号] ");
                } else if (6 == step) {
                    goddess.setMobile(in);

                    try {
                        action.add(goddess);
                        System.out.println("新增女神成功！");
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("新增女神失败！");
                    } finally {
                        // 重置
                        prenious = null;
                        step = 1;
                    }
                }

                if (OPERATION_ADD.equals(prenious)) {
                    step++;
                }

            } else if (OPERATION_UPDATE.equals(in.toUpperCase())
                    || OPERATION_UPDATE.substring(0, 1).equals(in.toUpperCase()) || OPERATION_UPDATE.equals(prenious)) {

                prenious = OPERATION_UPDATE;

                if (OPERATION_BREAK.equals(in.toUpperCase())
                        || OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
                    System.out.println(CONTEXT);
                    // 重置
                    prenious = null;
                    step = 1;
                    continue;
                }

                // 编辑女神
                if (1 == step) {
                    System.out.println("请输入要更新的女神编号\n编号:");
                } else if (2 == step) {
                    try {
                        goddess = action.get(Integer.valueOf(in));
                        goddess.setId(Integer.valueOf(in));
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    System.out.println("姓名：（如何不更新该字段，则输入null）：");
                } else if (3 == step) {
                    if (!"null".equals(in)) {
                        goddess.setUser_name(in);
                    }
                    System.out.println("年龄：");
                } else if (4 == step) {
                    goddess.setAge(Integer.valueOf(in));
                    System.out.println("性别：（0：女，1：男）");
                } else if (5 == step) {
                    goddess.setSex(Integer.valueOf(in));
                    System.out.println("生日：（如：1995-03-15）");
                } else if (6 == step) {
                    if (!"null".equals(in)) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                        Date birthday = null;
                        try {
                            birthday = sdf.parse(in);
                            goddess.setBirthday(birthday);
                        } catch (ParseException e) {
                            e.printStackTrace();
                            System.out.println("您输入的格式有误，请重新输入！");
                            step = 5;
                        }
                    }
                    System.out.println("邮箱：");
                } else if (7 == step) {
                    if (!"null".equals(in)) {
                        goddess.setEmail(in);
                    }
                    System.out.println("手机号：");
                } else if (8 == step) {
                    if (!"null".equals(in)) {
                        goddess.setMobile(in);
                    }
                    try {
                        action.edit(goddess);
                        System.out.println("编辑女神成功！");
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.out.println("编辑女神失败！");
                    } finally {
                        // 重置
                        prenious = null;
                        step = 1;
                    }
                }

                if (OPERATION_UPDATE.equals(prenious)) {
                    step++;
                }

            } else if (OPERATION_DELETE.equals(in.toUpperCase())
                    || OPERATION_DELETE.substring(0, 1).equals(in.toUpperCase()) || OPERATION_DELETE.equals(prenious)) {

                prenious = OPERATION_DELETE;

                if (OPERATION_BREAK.equals(in.toUpperCase())
                        || OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
                    System.out.println(CONTEXT);
                    // 重置
                    prenious = null;
                    step = 1;
                    continue;
                }

                // 删除女神
                if (1 == step) {
                    System.out.println("请输入要删除的女神的编号，可以删除多个，以逗号分隔，如1，2");
                } else if (2 == step) {
                    String[] ids = in.split(",");
                    if (ids != null && ids.length > 0) {
                        for (int i = 0; i < ids.length; i++) {
                            try {
                                action.del(Integer.valueOf(ids[i]));
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("删除女神完毕！");
                        // 重置
                        prenious = null;
                        step = 1;
                    }
                }

                if (OPERATION_DELETE.equals(prenious)) {
                    step++;
                }

            } else if (OPERATION_SEARCH.equals(in.toUpperCase())
                    || OPERATION_SEARCH.substring(0, 1).equals(in.toUpperCase()) || OPERATION_SEARCH.equals(prenious)) {

                prenious = OPERATION_SEARCH;

                if (OPERATION_BREAK.equals(in.toUpperCase())
                        || OPERATION_BREAK.substring(0, 1).equals(in.toUpperCase())) {
                    System.out.println(CONTEXT);
                    // 重置
                    prenious = null;
                    step = 1;
                    continue;
                }

                // 检索女神
                if (1 == step) {
                    System.out.println("请选择要查找的字段\n[NAME] 姓名 \n[MOBILE] 手机号");
                } else if (2 == step) {
                    if ("name".equals(in.toLowerCase())) {
                        param.put("name", "user_name");
                        queryKey = in;
                        System.out.println("请输入要查询的姓名：");
                    } else if ("mobile".equals(in.toLowerCase())) {
                        param.put("name", "mobile");
                        queryKey = in;
                        System.out.println("请输入要查询的手机号：");
                    }
                } else if (3 == step) {
                    if ("name".equals(queryKey)) {
                        param.put("value", "'" + in + "'");
                    } else if ("mobile".equals(queryKey)) {
                        param.put("value", "'" + in + "'");
                    }

                    params.add(param);
                    try {
                        List<Goddess> resultList = action.query(params);
                        if (resultList.size() == 0) {
                            System.out.println("未查询到女神信息。。。");
                        }
                        for (Goddess info : resultList) {
                            System.out.println("编号：" + info.getId() + "\n姓名：" + info.getUser_name() + "\n年龄："
                                    + info.getAge() + "\n性别：" + (info.getSex() == 1 ? "男" : "女") + "\n生日："
                                    + info.getBirthday() + "\n邮箱：" + info.getEmail() + "\n手机号：" + info.getEmail());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();

                    } finally {
                        // 重置
                        prenious = null;
                        step = 1;
                        queryKey = null;
                    }
                }

                if (OPERATION_SEARCH.equals(prenious)) {
                    step++;
                }

            } else if (OPERATION_EXIT.equals(in.toUpperCase())
                    || OPERATION_EXIT.substring(0, 1).equals(in.toUpperCase())) {
                System.out.println("您已退出女神禁区。。。");
                break;
            } else {
                System.out.println("您输入的值是：" + in);
            }

        }

        scan.close();
    }

}
